//
//  ArtTypeView.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-2.
//
//

#import "ArtTypeView.h"

@implementation ArtTypeView
{
    id<artTypeViewDelegate> _typedelegate;
}

- (id)initWithFrame:(CGRect)frame andDelegate:(id<artTypeViewDelegate>)typedelegate
{
    _typedelegate = typedelegate;
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        // 设置tableView的数据源
        self.dataSource = self;
        // 设置tableView的委托
        self.delegate = self;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.allArtInfo.artList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
    
    ArtInfo *artInfo = [self.allArtInfo.artList objectAtIndex:indexPath.row];
    
    //加载图像
    UIImageView *imgview  = [[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 70, 80)]autorelease];
    NSURL *photourl = [NSURL URLWithString:artInfo.ImageUrl];
    //url请求实在UI主线程中进行的
    UIImage *images = [UIImage imageWithData:[NSData dataWithContentsOfURL:photourl]];//通过网络url获取uiimage
    imgview.image = images;
    [cell.contentView addSubview:imgview];
    
    //加载名字
    UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 220, 20)]autorelease];
    namelbl.text = artInfo.ProjectName;
    namelbl.font = [UIFont systemFontOfSize:15];
    namelbl.adjustsFontSizeToFitWidth = YES;
    [cell.contentView addSubview:namelbl];
    
    //加载地点
    UILabel *addresslbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 30, 150, 20)]autorelease];
    addresslbl.text = artInfo.FieldName;
    addresslbl.font = [UIFont systemFontOfSize:10];
    addresslbl.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:addresslbl];
    
    //加载场馆
    UILabel *gemlbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 50, 150, 20)]autorelease];
    gemlbl.text = artInfo.Address;
    gemlbl.font = [UIFont systemFontOfSize:10];
    gemlbl.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:gemlbl];
    
    //加载时间
    UILabel *timelbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 70, 150, 20)]autorelease];
    timelbl.text = artInfo.PerformTime;
    timelbl.font = [UIFont systemFontOfSize:10];
    timelbl.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:timelbl];
    
    //加载价格
    
    int price = 99999999;
    for (int i = 0; i < [artInfo.priceInfoArray count]; i++) {
        PriceInfo *priceInfo = [artInfo.priceInfoArray objectAtIndex:i];
        int priceValue =  [priceInfo.Price intValue];
        if (price > priceValue) {
            price = priceValue;
        }
    }
    UILabel *pricelbl = [[[UILabel alloc]initWithFrame:CGRectMake(260, 50, 60, 40)]autorelease];
    pricelbl.text = [NSString stringWithFormat:@"￥%d", price];
    pricelbl.font = [UIFont systemFontOfSize:20];
    pricelbl.textColor = [UIColor redColor];
    pricelbl.adjustsFontSizeToFitWidth = YES;
    [cell.contentView addSubview:pricelbl];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_typedelegate clickArtTypeView:[self.allArtInfo.artList objectAtIndex:indexPath.row]];
}

@end
